'use strict'

$(()=>{
    let url=location.search;
    if (url.length>0) {
        let id=(url.split('='))[1]
        $.get(`${baseUrl}/books/${id}`,(res)=>{
            let obj=res.data;
            $('#trId').show();
            $('[name=id]').attr('value',obj.id);
            $('[name=name]').attr('value',obj.name);
            $('[name=price]').attr('value',obj.price);
            $('[name=press]').attr('value',obj.press);
            $('#btnSave').attr('value','更新');
        })
    }
})

function btnSave(){
    let url=location.search;
    if (url.length>0) {
        let id=(url.split('='))[1]
        let tmpObj={
            name:$('[name=name]').val(),
            price:$('[name=price]').val(),
            press:$('[name=press]').val()
        }
        $.ajax({
            type:"put",
            url:`${baseUrl}/books/${id}`,
            data:tmpObj,
            success:(res)=>{
                if (res.code=1000) {
                    window.location.href='./index.html'
                }else{
                    alert(msg);
                }
            }
        })
    }else{
        let tmpObj={
            name:$('[name=name]').val(),
            price:$('[name=price]').val(),
            press:$('[name=press]').val()
        }
        $.post(`${baseUrl}/books`,tmpObj,(res)=>{
            if (res.code=1000) {
                window.location.href='./index.html'
            }else{
                alert(msg);
            }
        })
    }
    
}


function btnCancel(){
    window.location.href='./index.html'
}